package des;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.Socket;
import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;

public class Decryptfile 
{
	 Cipher c;
	 Socket cs;
	 private String fname;
	 private String path;
	 private String fname1;
	 
	 //get file name for decryption
	 public String getFileName() throws Exception
	 {
		 String path;
		 String filepath;
		 System.out.println("Enter the name of file");
		 BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
		 filepath=br.readLine();
		 path=filepath;
		 return path;	
	 }	
	 
	 //set file name for the decrypted file
	 public void setfname(String fname)
	 {
		 this.fname=fname;
	 }
	 
	 //set path for the file to be decrypted
	 public void setpath(String path)
	 {
		 this.path=path;
	 }
	
	 //decrypts file
	 public void Decrypt(Key key) throws Exception
	 {
		 c = Cipher.getInstance("DES");
		 fname1=fname;
         //initialize the mode
		 c.init(Cipher.DECRYPT_MODE,key);
         //pass the file through cipherinputstream
		 CipherInputStream cis = new CipherInputStream(new FileInputStream((this.path)),c);
         File ftarget=new File(fname1);
		 ftarget.createNewFile();
		 FileOutputStream fos1 = new FileOutputStream(fname1);
		 int j;
		 while((j=cis.read())!=-1)
		 {
			 //write decrypted data to a file
			 fos1.write(j);
		 }
		 //close the fileoutputstream
		 fos1.close();
	 }
	 
	 //get the file name
	 public String getdfilename()
	 {
		 return this.fname1;
	 }
}
